#include "os_config.h"
#include "os_type.h"
#include "os_task.h"
#include "os_task_com.h"
#define  OS_CM3_NVIC_ST_CTRL    (*((volatile unsigned int *)0xE000E010))   
#define  OS_CM3_NVIC_ST_RELOAD  (*((volatile unsigned int *)0xE000E014))   
#define  OS_CM3_NVIC_SYS_PRIO	(*(volatile unsigned long *) 0xe000ed20 )

#define  OS_CM3_NVIC_ST_CTRL_CLK_SRC 0x00000004     
#define  OS_CM3_NVIC_ST_CTRL_INTEN   0x00000002    
#define  OS_CM3_NVIC_ST_CTRL_ENABLE  0x00000001    

static void priOSCm3TickInit(void)
{
    OS_CM3_NVIC_ST_RELOAD = (72000000/OS_CONFIG_TICK_RATE_HZ - 1);                                   
    OS_CM3_NVIC_ST_CTRL  |= OS_CM3_NVIC_ST_CTRL_CLK_SRC | OS_CM3_NVIC_ST_CTRL_INTEN | OS_CM3_NVIC_ST_CTRL_ENABLE ;
}

static void priOSCm3IsrPrioInit(void)
{
	OS_CM3_NVIC_SYS_PRIO |= ((unsigned long)OS_CONFIG_PENDSV_PRIO<16);
	OS_CM3_NVIC_SYS_PRIO |= ((unsigned long)OS_CONFIG_SYSTICK_PRIO<<24);
}

void OSM3Init(void)
{
	priOSCm3TickInit();
	priOSCm3IsrPrioInit();
}

void SysTick_Handler(void)
{
	OSTaskTick();
}

__asm void priOSM3IntMask(void)
{
	PRESERVE8

	PUSH { R0 }
	MOV R0, #OS_CONFIG_SUPER_ISR_PRIO
	MSR basepri, R0
	POP { r0 }
	BX  LR
}
__asm void priOSM3ClearIntMask(void)
{
	PRESERVE8

	PUSH { R0 }
	MOV R0, #0
	MSR basepri, R0
	POP { R0 }
	BX LR
}
static u32 priOSM3CriticalNesting = 0;
void OSEnterCritical( void )
{
	priOSM3IntMask();
	priOSM3CriticalNesting++;
}
void OSExitCritical( void )
{
	priOSM3CriticalNesting--;
	if( priOSM3CriticalNesting == 0 )
		priOSM3ClearIntMask();
}




